package com.teacher.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.teacher.common.CommonResult;
import com.teacher.common.JwtTokenUtil;
import com.teacher.pojo.dto.healthDevice.HealthDeviceAddDto;
import com.teacher.pojo.dto.healthDevice.HealthDeviceUpdateDto;
import com.teacher.pojo.entity.HealthDevice;
import com.teacher.pojo.entity.HealthDevice;
import com.teacher.mapper.HealthDeviceMapper;
import com.teacher.service.IHealthDeviceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jiangzhuohang
 * @since 2025-04-12
 */
@Service
public class HealthDeviceServiceImpl extends ServiceImpl<HealthDeviceMapper, HealthDevice> implements IHealthDeviceService {

    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @Autowired
    private HttpServletRequest httpServletRequest;
    
    @Autowired
    private HealthDeviceMapper healthDeviceMapper;
    
    @Override
    public CommonResult getHealthDevice() {
        String username = jwtTokenUtil.getUsernameByRequest(httpServletRequest);
        List<HealthDevice> healthDevices = healthDeviceMapper.selectList(new QueryWrapper<HealthDevice>().lambda().eq(HealthDevice::getUserName, username));
        
        return CommonResult.success(healthDevices);
    }

    @Override
    public CommonResult addHealthDevice(HealthDeviceAddDto dto) {
        String username = jwtTokenUtil.getUsernameByRequest(httpServletRequest);
        HealthDevice healthDevice = new HealthDevice();
        healthDevice.setUserName(username);
        BeanUtil.copyProperties(dto,healthDevice);
        int insert = healthDeviceMapper.insert(healthDevice);
        if (insert<1) {
            return CommonResult.failed("新增失败");
        }
        return CommonResult.success("新增成功");
    }

    @Override
    public CommonResult deleteHealthDevice(List<Long> list) {
        int i = healthDeviceMapper.deleteBatchIds(list);
        if (i<1){
            return CommonResult.failed("删除失败");
        }
        return CommonResult.success("删除成功");
    }

    @Override
    public CommonResult updateHealthDevice(HealthDeviceUpdateDto updateDto) {
        HealthDevice healthDevice = new HealthDevice();
        BeanUtil.copyProperties(updateDto,healthDevice);
        int i = healthDeviceMapper.updateById(healthDevice);
        if (i<1){
            return CommonResult.failed("更新失败");
        }
        return CommonResult.success("更新成功");
    }
}
